package com.hot100.easy;

import java.util.Stack;

/**
 * @ClassName Demo15
 * @Description  反转链表
 *
 * @Author zt
 * @Date 15:46 2022/6/11
 * @Version 1.0
 **/
public class Demo15 {
    public static void main(String[] args) {

    }


    public class ListNode {
          int val;
          ListNode next;
          ListNode() {}
          ListNode(int val) { this.val = val; }
          ListNode(int val, ListNode next) { this.val = val; this.next = next; }
      }

    public ListNode reverseList(ListNode head) {
        Stack<Integer> stack=new Stack<>();
        while (head!=null){
            stack.push(head.val);
            head=head.next;
        }
        ListNode newNode=new ListNode();
        ListNode temp=newNode;
        while (!stack.empty()){
            Integer pop = stack.pop();
            newNode.next=new ListNode(pop);
            newNode=newNode.next;
        }
        return temp.next;

    }

}